import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * The type Recipe recommendation frame.
 *
 * @author Yihan Wang
 * @date 24/06/11
 */
public class RecipeRecommendationFrame extends JFrame implements ActionListener {
    /**
     * The Back.
     */
    JButton back = new JButton("Back");
    /**
     * The Button panel.
     */
    JPanel buttonPanel = new JPanel();
    /**
     * The Panel.
     */
    JPanel panel = new JPanel();
    /**
     * The Background label.
     */
    JLabel backgroundLabel = new JLabel();
    /**
     * The Recipe 1.
     */
    JTextArea recipe1 = new JTextArea();
    /**
     * The Recipe 2.
     */
    JTextArea recipe2 = new JTextArea();
    /**
     * The Recipe 3.
     */
    JTextArea recipe3 = new JTextArea();
    /**
     * The Cook button 1.
     */
    JButton cookButton1 = new JButton("Cook This");
    /**
     * The Cook button 2.
     */
    JButton cookButton2 = new JButton("Cook This");
    /**
     * The Cook button 3.
     */
    JButton cookButton3 = new JButton("Cook This");
    /**
     * The Top 3 recipes.
     */
    public List<Recipe> top3Recipes;

    /**
     * Instantiates a new Recipe recommendation frame.
     */
    public RecipeRecommendationFrame() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                top3Recipes = Main.rc1.getTop3Recipes();
                Interface();
                element();
                displayRecipes();
            }
        });
        this.setVisible(true);
    }

    /**
     * Interface.
     */
    public void Interface() {
        this.setTitle("Recipe recommendation page");
        this.pack();
        this.setSize(new Dimension(1200, 675));
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.getContentPane().add(buttonPanel);
        this.setLayout(new BorderLayout());

        Path filePath = Paths.get(System.getProperty("user.dir"), "resources/recipeRecommendation.png");
        String recipeRecommendationFilePath = filePath.toString();

        backgroundLabel = new JLabel(new ImageIcon(recipeRecommendationFilePath));
        backgroundLabel.setBounds(0, 0, 1200, 675);
        backgroundLabel.setLayout(new BorderLayout());
        backgroundLabel.add(buttonPanel);
        backgroundLabel.setVisible(true);
        panel.add(backgroundLabel);
        this.getContentPane().add(panel);
    }

    /**
     * Element.
     */
    public void element() {
        back.setBounds(25, 25, 80, 80);
        back.setFont(new Font("Posterama", Font.PLAIN, 20));
        back.setBackground(Color.orange);
        back.addActionListener(this);

        setupTextArea(recipe1, 85, 100, 300, 475);
        setupTextArea(recipe2, 450, 100, 300, 475);
        setupTextArea(recipe3, 850, 100, 300, 475);

        setupCookButton(cookButton1, 75, 590, 300, 30);
        setupCookButton(cookButton2, 450, 590, 300, 30);
        setupCookButton(cookButton3, 850, 590, 300, 30);

        buttonPanel.setLayout(null);
        buttonPanel.setOpaque(false);
        buttonPanel.add(back);
    }

    private void setupTextArea(JTextArea textArea, int x, int y, int width, int height) {
        textArea.setFont(new Font("Posterama", Font.PLAIN, 18));
        textArea.setOpaque(false);
        textArea.setForeground(Color.BLACK);
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);

        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setBounds(x, y, width, height);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        scrollPane.getVerticalScrollBar().setValue(0);

        scrollPane.getVerticalScrollBar().setValue(0);
        scrollPane.revalidate();
        scrollPane.repaint();
        buttonPanel.add(scrollPane);

    }

    private void setupCookButton(JButton button, int x, int y, int width, int height) {
        button.setBounds(x, y, width, height);
        button.setFont(new Font("Posterama", Font.PLAIN, 18));
        button.setBackground(Color.orange);
        button.addActionListener(this);
        buttonPanel.add(button);
    }

    /**
     * Display recipes.
     */
    public void displayRecipes() {
        JTextArea[] recipes = {recipe1, recipe2, recipe3};
        JButton[] cookButtons = {cookButton1, cookButton2, cookButton3};

        for (int i = 0; i < top3Recipes.size(); i++) {
            Recipe recipe = top3Recipes.get(i);
            JTextArea recipeArea = recipes[i];
            recipeArea.setText(recipe.printRecipeInfo());

            cookButtons[i].setActionCommand(String.valueOf(i));
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();

        if (obj == back) {
            this.setVisible(false);
            new MenuFrame();
        } else if (obj == cookButton1 || obj == cookButton2 || obj == cookButton3) {
            int index = Integer.parseInt(e.getActionCommand());
            Recipe recipe = top3Recipes.get(index);
            String result = Main.rc1.cookRecipe(Main.fc1, recipe);
            showResultDialog(result);
        }
    }

    private void showResultDialog(String message) {
        JDialog dialog = new JDialog(this, "Cook Result", true);
        dialog.setSize(400, 200);
        dialog.setLayout(new BorderLayout());

        JTextArea messageArea = new JTextArea(message);
        messageArea.setFont(new Font("Posterama", Font.PLAIN, 18));
        messageArea.setEditable(false);
        messageArea.setLineWrap(true);
        messageArea.setWrapStyleWord(true);
        dialog.add(new JScrollPane(messageArea), BorderLayout.CENTER);

        JButton okButton = new JButton("OK");
        okButton.setFont(new Font("Posterama", Font.PLAIN, 18));
        okButton.addActionListener(e -> {
            dialog.dispose();
            this.setVisible(false);
            new MenuFrame();
        });
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(okButton);
        dialog.add(buttonPanel, BorderLayout.SOUTH);

        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }
}